﻿using IRepositoryBll;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RepositoryCommon;
using RepositoryEntity;
using RepositorySystem.Models;
using RepositorySystem.Models.UserInfoViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace RepositoryAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserInfoController : ControllerBase
    {
        private IUserInfoBll _userInfoBll;
        private IDepartmentInfoBll _departmentInfoBll;
        public UserInfoController(IUserInfoBll userInfoBll, IDepartmentInfoBll departmentInfoBll)
        {
            _userInfoBll = userInfoBll;
            _departmentInfoBll = departmentInfoBll;
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddUserInfo(AddUserInfoViewModel model)
        {
            CustomActionResult res = new CustomActionResult();
            JsonResult result;
            result = new JsonResult(res);


            if (string.IsNullOrEmpty(model.Account))
            {
                res.Msg = "账号不能为空";
                return result;
            }
            else if (string.IsNullOrEmpty(model.PassWord))
            {
                res.Msg = "密码不能为空";
                return result;
            }
            else if (string.IsNullOrEmpty(model.UserName))
            {
                res.Msg = "用户名不能为空";
                return result;
            }

            UserInfo entity = new UserInfo()
            {
                ID = Guid.NewGuid().ToString(),
                Account = model.Account,
                UserName = model.UserName,
                PhoneNum = model.PhoneNum,
                Email = model.Email,
                Sex = model.Sex,
                IsAdmin = model.IsAdmin,
                //添加用户数据时，对密码进行MD5加密
                PassWord = MD5Help.md5(model.PassWord),
                CreateTime = DateTime.Now,
                IsDelete = false,
                DepartmentID = model.DepartmentId
            };

            bool isSuccess = _userInfoBll.Add(entity);
            if (isSuccess)
            {
                res.IsSuccess = true;
                res.Msg = "添加成功";
                return result;
            }
            else
            {
                return result;
            }
        }

        /// <summary>
        /// 获取更新用户页面信息
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetUpdateUserInfo(string userInfoId)
        {
            CustomActionResult res = new CustomActionResult();
            JsonResult result;
            result = new JsonResult(res);
            //获取用户信息
            UserInfo userInfo = _userInfoBll.GetEntity(userInfoId);
            //查询部门信息下拉选数据
            object departmentInfos = _departmentInfoBll.GetDepartmentSelectOption();
            res.IsSuccess = true;
            res.Status = 1;
            res.Msg = "成功";
            res.Data = new
            {
                UserInfo = userInfo,
                DepartmentInfos = departmentInfos
            };
            return result;
        }


        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateUserInfo(UpdateUserInfoViewModel model)
        {
            CustomActionResult res = new CustomActionResult();
            JsonResult result;
            result = new JsonResult(res);
            if (_userInfoBll.UpdateUserInfo(model.ID, model.UserName, model.Sex, model.PhoneNum, model.DepartmentId))
            {
                res.IsSuccess = true;
                res.Msg = "添加成功";
                return result;
            }
            else
            {
                return result;
            }
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult DeleteUserInfo(string userInfoId)
        {

            if (_userInfoBll.SoftDeleteEntity(userInfoId))
            {
                return new JsonResult("OK");
            }
            else
            {
                return new JsonResult("Error");
            }
        }

        /// <summary>
        /// 根据id获取用户信息
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetUserInfo(string userInfoId)
        {
            UserInfo userInfo = _userInfoBll.GetEntity(userInfoId);
            if (userInfo == null)
            {
                return new JsonResult("Error");
            }
            else
            {
                return new JsonResult(userInfo);
            }
        }

        /// <summary>
        /// /获取用户集合
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetUserInfoList(int page, int limit, string userName, string phoneNum)
        {
            int count;

            object userInfos = _userInfoBll.GetUserInfoListByPage(page, limit, out count, userName, phoneNum);

            return new JsonResult(new
            {
                code = 0,
                msg = "成功",
                count = count,
                data = userInfos
            });
        }

        /// <summary>
        /// 获取部门下拉集合
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetDepartmentSelectOption()
        {
            CustomActionResult res = new CustomActionResult();
            JsonResult result;
            result = new JsonResult(res);
            //查询部门集合
            object options = _departmentInfoBll.GetDepartmentSelectOption();
            res.IsSuccess = true;
            res.Msg = "成功";
            res.Status = 1;
            res.Data = options;
            return result;
        }
    }
}
